package com.yun.zhong.fu.sharecenter.origionMode;

import com.yun.zhong.fu.sharecenter.annoation.MyCompent;
import jdk.nashorn.internal.ir.CallNode;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.io.File;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author RenPu
 * @Date 2022/10/25 17:33
 * @Version 1.0
 * @Description: 模拟编写spring上下文对象
 **/
public class ApplicationContext {

    /**
     * IOC容器
     */
     Map<String, Object> iocContainer=new ConcurrentHashMap<>();



    /**
     * 从IOC容器里获取bean对象
     */
    public Object getObject(String beanName){
        return iocContainer.get(beanName);
    }

    /**
     * 构造方法传入包路径
     */
    public ApplicationContext(String packagePath) {

        //扫描指定包路径
        scanPackage(packagePath);
    }



    /**
     * 扫描包路径进行解析过滤
     */
    private void scanPackage(String packagePath) {
        File[] files=getClassFiles(packagePath);
        processClassFiles(packagePath,files);

    }

    private void processClassFiles(String packagePath, File[] files) {

        //1:遍历符合文件的集合进行，决定路径的出路
        for (File file : files) {
            String className = file.getName().substring(0, file.getName().lastIndexOf("."));

            //拼接出全限定类名，后面反射需要用到
            String fullyClassName =packagePath+"."+className;

            //将类的首字母小写，后面作为beanName存放到IOC容器
            String beanName=String.valueOf(className.charAt(0)).toLowerCase()+className.substring(1);

            //创建并存到IOC容器
            creatBean(beanName,fullyClassName);
        }


    }

    private void creatBean(String beanName, String fullyClassName) {

        try {
            Class<?> aClass = Class.forName(fullyClassName);

            if(aClass.isAnnotationPresent(MyCompent.class)){
                System.out.println("fullyClassName  ="+ fullyClassName + "加了 MyCompent注解，准备通过反射创建，放入到IOC容器！！！！");
                Object instance = aClass.newInstance();
                iocContainer.put(beanName,instance);
            }else {
                System.out.println("fullyClassName  ="+ fullyClassName + "没有加 MyCompent注解，无须创建！！！！");

            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    private File[] getClassFiles(String packagePath) {
        String path = packagePath.replaceAll("\\.", "/");  //替换符号
        URL url = getClass().getClassLoader().getResource(path); //获取包路径
        File file = new File(url.getFile());                    //获取文件对象

        return filterClassFiles(packagePath,file);             //过滤符合的文件
    }

    private File[] filterClassFiles(String path,File file) {
        return file.listFiles(f -> {
            String name = f.getName();
            if(f.isDirectory()){
                scanPackage(path+"."+name);
            }else {
                if(name.endsWith(".class")){
                    return true;
                }
            }
            return false;
        });


    }


   public static void main(String[] args) {
//        String path="com.yun.zhong.fu.sharecenter.origionMode";
//        ApplicationContext applicationContext = new ApplicationContext(path);
//        Student student =(Student) applicationContext.getObject("student");
//        student.eat();

       AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.yun.zhong.fu.sharecenter.origionMode");
       Tearche tearche =(Tearche) context.getBean("tearche");
       System.out.println("老师得名称为："+tearche.getName());

   }

}
